package com.superlz.demo.security.web.controller;

import java.util.HashMap;
import java.util.Map;

import com.superlz.demo.security.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 统一异常拦截处理
 *
 * lz：@ControllerAdvice：Controller的切面
 *
 * @author zhailiang
 *
 */
@ControllerAdvice
public class ControllerExceptionHandler {

	/**
	 * @ExceptionHandler(要拦截的异常)
	 *
	 * @param ex
	 * @return 响应返回的信息
	 */
	@ExceptionHandler(UserNotExistException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public Map<String, Object> handleUserNotExistException(UserNotExistException ex) {
		Map<String, Object> result = new HashMap<>();
		result.put("id", ex.getId());
		result.put("message", ex.getMessage());
		return result;
	}

}
